<?php

class editCustomer extends bee\form
{ protected static $__formID = "frm201201180954";
  public $__caption = "Edit customer data";
  public $__version = "1.0";
  protected $__activeMenu = "customerOverview";

  protected $error;
  protected $customer;
  protected $customer_ext;
  protected $client;

  protected $grid;
  protected $arr_stat;

  protected $tID;
  protected $tName;
  protected $tDescription;
  protected $tVAT;
  protected $oStatus;

  public function createForm($evt)
  { $this->customer = new bee\org_unit();
    $this->customer_ext = new org_unit_ext();
    $this->client = new bee\user();
    if(isset($evt["id"]))
    { $this->customer->SELECT($evt["id"]);
      $this->customer_ext->SELECT($evt["id"]);
      $this->client->org_id = $evt["id"];
    }

    $this->arr_stat = bee\getListByRange(1, "lvalue", "ltext");
    $this->grid = clientGrid::getInstance();
    $this->grid->setDataSource($this->client);

    $this->tID           = new bee\text("id",           $this->customer->id);
    $this->tName         = new bee\text("name",         $this->customer->name);
    $this->tDescription  = new bee\text("description",  $this->customer->description);
    $this->tVAT          = new bee\text("VAT",          $this->customer_ext->VAT);
    $this->oStatus       = new bee\option("status",     $this->customer->status, $this->arr_stat);
    $this->oStatus->id   = "statuss";
    $this->oStatus->notSelectedValue = "";
    $this->oStatus->notSelectedText  = "";
  }

  public function loadForm($evt)
  { $this->error =      new bee\serror();
  }

  public function drawForm($evt)
  { $this->__DSDoc->setRow($this->customer);
  }

  public function update($evt)
  { $this->customer->fillFromArray($evt);
    $this->customer_ext->fillFromArray($evt);
    if($this->validate($this->customer, $this->error))
    { $this->customer->UPDATE();
      $this->customer_ext->UPDATE();
      //die(print_r($this->__app));
      $this->__app->setMessage("Customer data modified.");

    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_ERROR);
    }
  }

  public function delete($evt)
  { $this->customer->fillFromArray();
    $user = new bee\user();
    $user->org_id = $this->customer->id;
    $rs_user = new bee\rowSet($user);
    if($rs_user->getRow())
    { bee\app::setMessage("Customer " . $this->customer->name . " can't be deleted because he owns users.", MSG_LEVEL_ERROR);
    } else
    { $this->customer->DELETE();
      $this->customer_ext->DELETE();
      bee\app::setMessage("Customer " . $this->customer->name . " deleted.");
    }
    header("Location: index.php?__form=" . $this->__parent . "&sys_msg=" . urlencode(bee\app::getMessageText())
                     . "&sys_msg_style=" . bee\app::getMessageLevel());
  }

  public function add($evt)
  { $this->customer->fillFromArray();
    $this->customer_ext->fillFromArray();

    if(!$this->validate($this->customer, $this->error))
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_ERROR);
    } else
    { $this->customer->INSERT();
      $this->customer_ext->id = $this->customer->id;
      $this->customer_ext->INSERT();
      bee\app::setMessage("Customer added.");
    }
    $this->client->org_id = $this->customer->id;
  }

  protected function validate(bee\org_unit $row, bee\serror &$error)
  { if(!$row->name)
    { $error->err_code = -10;
      $error->err_description = "Name must be given.";
      return false;
    }
    $old_client = new bee\org_unit();
    $old_client->name = $row->name;
    $rs_client = new bee\rowSet($old_client);
    if($rs_client->getRow($old_client) && $old_client->id != $row->id)
    { $error->err_code = -20;
      $error->err_description = "There's allerady client with given name";
      return false;
    }
    $error->err_code = 0;
    return true;
  }
}

class clientGrid extends bee\grid
{ public function createGrid($evt)
  { $this->__gridID = "grd201101281010";
    $this->setColumnProperties(array("name" => "login", "caption" => "Login", "width" => 100));
    $this->setColumnProperties(array("name" => "description", "caption" => "Description", "width" => 300));
    $this->setColumnProperties(array("name" => "last_login_date", "caption" => "Last login", "width" => 150));
    $this->detailFormName="userData";
    $this->showButtons = GRID_SHOW_ALL;
  }
}







